package com.adou.d8.thread.comm;

import java.util.ArrayList;
import java.util.List;

public class Desk {
    private List<String> list = new ArrayList<>();


    // 放包子 厨师1 厨师2 厨师3
    public void out(){
        try {
            String name = Thread.currentThread().getName();
            if(list.size() == 0) {
                list.add(name + "肉包子");
                System.out.println(name + "肉包子");
                Thread.sleep(2000);

                // 唤醒别人，等待自己
                this.notifyAll();
                this.wait();
            } else {
                // 唤醒别人，等待自己
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // 吃包子 吃货1 吃货2
    public void in(){
        try {
            String name = Thread.currentThread().getName();
            if(list.size() >= 1) {
                // 有包子，吃了
                System.out.println(name + "吃了，" + list.get(0));
                list.clear();
                Thread.sleep(2000);

                this.notifyAll();
                this.wait();
            } else {
                // 没有包子
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
